package com.huishu.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.pojo.dto.UrlManageDTO;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.user.pojo.dto.UrlManagePageDTO;
import com.huishu.user.pojo.model.UrlManageDO;
import com.huishu.user.service.UrlManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author generator@ljj
 * @since 2023-06-16
 */
@RestController
@RequestMapping("/url-manage")
public class UrlManageController {

    @Autowired
    private UrlManageService urlManageService;

    @PostMapping("/pageList")
    public Result pageList(@RequestBody UrlManagePageDTO urlManagePageDTO) {
        return urlManageService.pageList(urlManagePageDTO);
    }

    @PostMapping("/create")
    public Result create(@RequestBody UrlManageDTO urlManageDTO) {
        UrlManageDO urlManageDO = BeanUtils.copyProperties(urlManageDTO, UrlManageDO.class);
        return urlManageService.save(urlManageDO) ? Result.success() : Result.fail();
    }

    @PostMapping("/update")
    public Result update(@RequestBody UrlManageDTO urlManageDTO) {
        LambdaUpdateWrapper<UrlManageDO> uw = Wrappers.lambdaUpdate(UrlManageDO.class)
                .set(UrlManageDO::getName, urlManageDTO.getName())
                .eq(UrlManageDO::getUrl, urlManageDTO.getUrl());
        return urlManageService.update(uw) ? Result.success() : Result.fail();
    }

    @PostMapping("/modify")
    public Result modify(@RequestBody UrlManageDTO urlManageDTO) {
        UrlManageDO urlManageDO = BeanUtils.copyProperties(urlManageDTO, UrlManageDO.class);
        return urlManageService.updateById(urlManageDO) ? Result.success() : Result.fail();
    }

    /**
     * 删除链接地址
     *
     * @param id
     * @return
     */
    @GetMapping("/url-manage/deleteUrl/{id}")
    public Result deleteUrl(@PathVariable Long id) {
        LambdaQueryWrapper<UrlManageDO> queryWrapper = Wrappers.lambdaQuery(UrlManageDO.class)
                .eq(UrlManageDO::getUrl, "/pages/goods/goodsDetails/goodsDetails?id=" + id);
        return urlManageService.remove(queryWrapper) ? Result.success() : Result.fail();
    }
}
